//
//  FFRecvPayPayVM.m
//  LLWallet
//
//  Created by tt on 2020/12/23.
//

#import "FFRecvPayPayVM.h"
#import "FFRecvPayPayInputCell.h"
#import "FFRecvPayPayUserCell.h"
#import "FFRecvPayPaySuccessVC.h"
#import "FFWalletManager.h"

@interface FFRecvPayPayVM ()
@property(nonatomic,copy) NSString *amountStr;
@property(nonatomic,copy) NSString *remarkStr;

@end

@implementation FFRecvPayPayVM




- (NSArray<NSDictionary *> *)tableSectionMaps {
   WKChannelInfo*channelInfo = [[WKSDK shared].channelManager getChannelInfo:[[WKChannel alloc] initWith:self.uid channelType:WK_PERSON]];
    __weak typeof(self) weakSelf = self;
    return @[
        @{
            @"height": @(0.01f),
            @"items": @[
                    @{
                        @"class": FFRecvPayPayUserModel.class,
                        @"avatar": [WKAvatarUtil getAvatar:self.uid],
                        @"name": channelInfo.displayName?:@"",
                    }
            ],
        },
        @{
            @"height": @(10.0f),
            @"items": @[
                    @{
                        @"class": FFRecvPayPayInputModel.class,
                        @"showBottomLine":@(YES),
                        @"onChange":^(NSString* value){
                            self.amountStr = value;
                        },
                        @"value": self.amount>0?[WKMoneyUtil penny2YuanFormat:@(self.amount)]:( self.amountStr?:@""),
                        @"inputEnable": @(self.amount<=0),
                        @"remark": weakSelf.remarkStr?:@"",
                        @"tip":self.remark?:@"",
                        @"onRemark":^(NSString *remark){ // 备注
                            weakSelf.remarkStr = remark;
                            [weakSelf reloadData];
                        },
                        @"onDone":^{
                            [self toPay];
                           
                            
                        }
                    }
            ],
        },
    ];
}

-(void) toPay {
    NSInteger amount = 0;
    if(self.amount>0) {
        amount = self.amount;
    }else{
        if(!self.amountStr || self.amountStr.floatValue<=0) {
            [[WKNavigationManager shared].topViewController.view showHUDWithHide:LLang(@"付款金额不能等于或小于0！")];
            return;
        }
        amount = [WKMoneyUtil yuan2Penny:@(self.amountStr.floatValue)].integerValue;
    }
   
    __weak typeof(self) weakSelf = self;
    [[FFWalletManager shared] ff_getPayToken:amount item:LIMPayItemQRCodePay remark:LLang(@"付款") callback:^(NSString * _Nonnull payToken) {
        [weakSelf pay:payToken amount:amount];
    }];
}

-(void) pay:(NSString*)payToken amount:(NSInteger)amount {
    [[WKNavigationManager shared].topViewController.view showHUD];
    [[WKAPIClient sharedClient] POST:@"impay/recvpay/receive" parameters:@{
        @"vercode": self.vercode?:@"",
        @"amount": @(amount),
        @"remark": self.remarkStr?:@"",
        @"pay_token": payToken?:@"",
    }].then(^{
        [[WKNavigationManager shared].topViewController.view hideHud];
        FFRecvPayPaySuccessVC *vc = [FFRecvPayPaySuccessVC new];
        vc.uid = self.uid;
        vc.amount = [WKMoneyUtil yuan2Penny:@(self.amountStr.floatValue)].integerValue;
        [[WKNavigationManager shared] pushViewController:vc animated:YES];
        
    }).catch(^(NSError *error){
        [[WKNavigationManager shared].topViewController.view switchHUDError:error.domain];
    });
}



@end
